package com.zhdl.modules.demo.testprocess;

import com.zhdl.common.ytools.callback.CommonCallback;
import com.zhdl.modules.common.warn.entity.WarnRecords;
import com.zhdl.modules.common.warn.service.RunWarnService;
import com.zhdl.modules.process.dto.ProcessBean;
import com.zhdl.modules.process.verify.DelayTimeVerify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class TestProcessInit {
    public boolean branch1Complete = false;
    public boolean branch2Complete = false;
    @Resource
    RunWarnService runWarnService;
    public List<ProcessBean> getTestMainProcess(CommonCallback<String> startChild, CommonCallback<String> startChild1) {
        List<ProcessBean> list = new ArrayList<>();
        list.add(new ProcessBean("测试流程1", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试流程1addDoSomeThing");
                })

                .addProcessVerify(new DelayTimeVerify(5000)));


        list.add(new ProcessBean("测试流程2", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试流程2addDoSomeThing");
                })
                .addProcessTimeoutCallBack(2000, (reason, processBean) -> {
                    log.info(reason);
                    runWarnService.addNewWarnService(new WarnRecords(1002,"测试流程2超时..."));
                })
                .addProcessVerify(new DelayTimeVerify(2000)));


        list.add(new ProcessBean("测试流程3", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试流程3addDoSomeThing 并且开启并行流程branch1");
                    startChild.statusCallback("ok");
                })
                .addProcessTimeoutCallBack(2000, (reason, processBean) -> {
                    log.info(reason);
                    runWarnService.addNewWarnService(new WarnRecords(1003,"测试流程3超时..."));
                })
                .addProcessVerify(new DelayTimeVerify(3000)));


        list.add(new ProcessBean("测试流程4", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试流程4addDoSomeThing");
                })
                .addProcessTimeoutCallBack(1000, (reason, processBean) -> {
                    log.info(reason);
                    runWarnService.addNewWarnService(new WarnRecords(1004,"测试流程4超时..."));
                })
                .addProcessVerify(new DelayTimeVerify(2000)));

        list.add(
                new ProcessBean("检测并行子流程是否完成", 1, 500)
                        .addDoSomeThing(() -> {
                        })

                        .addProcessVerify(() -> branch1Complete));


        list.add(new ProcessBean("测试流程5", 1, 500)
                .addDoSomeThing(() -> {
                    branch1Complete = false;
                    log.info("测试流程5addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));


        list.add(new ProcessBean("测试流程6", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试流程6addDoSomeThing 串行开启子流程2");
                })
                .addAfterProcess(lastProcess -> {
                    log.info("串行流程开启!!!!");
                    startChild1.statusCallback("ok");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));

        list.add(
                new ProcessBean("检测串行流程完成", 1, 500)
                        .addDoSomeThing(() -> {
                            log.info("检测串行流程完成开始执行");
                        })
                        .addProcessVerify(() -> branch2Complete));


        list.add(new ProcessBean("测试流程7", 1, 500)
                .addDoSomeThing(() -> {
                    branch2Complete = false;
                    log.info("测试流程7addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));

        list.add(new ProcessBean("测试流程8", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试流程8addDoSomeThing ");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试流程9", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试流程9addDoSomeThing ");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        return list;
    }

    public List<ProcessBean> getBranch1(CommonCallback<String> callback) {
        List<ProcessBean> list = new ArrayList<>();
        list.add(new ProcessBean("测试并行流程1", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试并行流程1addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试并行流程2", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试并行流程2addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试并行流程3", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试并行流程3addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试并行流程4", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试并行流程4addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试并行流程5", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试并行流程5addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测测试并行流程6", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试并行流程6addDoSomeThing");
                })
                .addAfterProcess(lastProcess -> {
                    branch1Complete = true;
                    callback.statusCallback("");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        return list;
    }

    public List<ProcessBean> getBranch2(CommonCallback<String> callback) {
        List<ProcessBean> list = new ArrayList<>();
        list.add(new ProcessBean("测试串行流程1", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试串行流程1addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试串行流程2", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试串行流程2addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试串行流程3", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试串行流程3addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试串行流程4", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试串行流程4addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试串行流程5", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测测试串行流程5addDoSomeThing");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        list.add(new ProcessBean("测试串行流程6", 1, 500)
                .addDoSomeThing(() -> {
                    log.info("测试串行流程6addDoSomeThing");
                })
                .addAfterProcess(lastProcess -> {
                    branch2Complete = true;
                    callback.statusCallback("");
                })
                .addProcessVerify(new DelayTimeVerify(2000)));
        return list;
    }
}
